/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Model.ConnectDatabase;
import Model.JobSeeker;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Duc Quyen
 */
public class loginjs extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        String jobID = request.getParameter("jobID");
        Connection con = ConnectDatabase.getConnection();
        java.sql.Statement sta;
        String url = "";
        try {
            sta = con.createStatement();

            String sql = "select * from jobseeker where Email='" + email + "' and Password='" + password + "'";
            boolean result = false;
            ResultSet re = sta.executeQuery(sql);
            JobSeeker js = null;
            while (re.next()) {
                int id = re.getInt(1);
                String name = re.getString(4);
                js = new JobSeeker(id, email, name);
                result = true;
            }
            // perform validation
            if (email == "") {
                request.setAttribute("error", "<span style='color:red'>Bạn chưa nhập email</span>");
            }
            if (password == "") {
                request.setAttribute("error", "<span style='color:red'>Bạn chua nhâp password</span>");
            }
            if (!result) {
                request.setAttribute("error", "<span style='color:red'>Email hoặc password không dúng</span>");
            }
            if (email.indexOf("@") == -1) {
                request.setAttribute("error", "<span style='color:red'>Email không hơp lệ</span>");
            }
            HttpSession session = request.getSession();
            session.setAttribute("jobseeker", js);
            System.out.println("ma so cong viec la" + jobID);
            if (!jobID.equals("null") & result) {
                url = "/detailjob.jsp?jobID=" + jobID;

            }
            if (result & jobID.equals("null")) {
                url = "/index.jsp";
            }
            if(!result){
                url = "/login.jsp";
            }
            System.out.println(jobID);
            System.out.println(url);

        } catch (SQLException ex) {
        }
        RequestDispatcher dispatcher = request.getRequestDispatcher(url);
        dispatcher.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
